package com.offcn.order.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.order.entity.Cart;
import com.offcn.order.service.CartService;
import com.offcn.order.vo.CartVo;
import com.offcn.sellergoods.pojo.Item;
import com.offcn.utils.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 查询购物车列表的数据
     */
    @RequestMapping("/findCartList")
    public Result<List<Cart>> findCartList(){
        // 获取令牌
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String token = details.getTokenValue();

        // 解析用户名
        String username = TokenDecode.getUsername(token);
        List<Cart> cartList = cartService.getCartListFromRedis(username);
        return new Result<>(true, StatusCode.OK,cartList,"查询成功");
    }

    /*
    添加购物车
     */
    @RequestMapping("/addItemToCartList")
    public Result addItemToCartList(@RequestBody Map<String,Object> map) {
      Long itemId=new Long ((Integer) map.get("itemId"));
      Integer num= (Integer) map.get("num");
        // 获取令牌
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String token = details.getTokenValue();

        // 解析用户名
        String username = TokenDecode.getUsername(token);

        // 查询加入之前的购物车列表
        List<Cart> cartList = this.findCartList().getData();
        cartList = cartService.addGoodsToCartList(itemId, num, cartList);
        // 将购物车存储到redis中
        cartService.saveCartListToRedis(username,cartList);
        //获取商品sku信息
        Item item = cartService.findItem(itemId);
        return new Result(true,StatusCode.OK,item,"添加购物车成功");
    }

    //批量添加购物车
    @PostMapping("/batchAddItemToCartList")
    public Result batchAddItemToCartList(@RequestBody JSONArray data){
        // 获取令牌
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String token = details.getTokenValue();

        // 解析用户名
        String username = TokenDecode.getUsername(token);
        List<CartVo> cartVoList = data.toJavaList(CartVo.class);
        Integer num=0;
        //判断购物车添加集合是否为空
        if(!CollectionUtils.isEmpty(cartVoList)) {
            for (CartVo cartVo : cartVoList) {
                //System.out.println(cartVo.getItemId() + " " + cartVo.getNum());
                //依次调用添加购物车
                // 查询加入之前的购物车列表
                List<Cart> cartList = this.findCartList().getData();
                cartList = cartService.addGoodsToCartList(new Long(cartVo.getItemId()), cartVo.getNum(), cartList);
                // 将购物车存储到redis中
                cartService.saveCartListToRedis(username,cartList);
                num++;
            }

        }

        if(num.intValue()==cartVoList.size()){
            return new Result(true, StatusCode.OK, "批量添加购物车成功");
        }else {
            return new Result(true, StatusCode.OK, "批量添加购物车失败");
        }

    }

}
